﻿
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Test.Ex1
{
    [TestClass]
    public class UnitTestXlinq
    {
        [TestMethod]
        public void Save_XDocument()
        {
            var xml = Repository.Document();
            var stringWriter = new StringWriter();
            var indentedTextWriter = new IndentedTextWriter(stringWriter);
            XmlWriter xmlWriter = new XmlTextWriter(indentedTextWriter);
            xml.Save(xmlWriter);

            var res = (stringWriter.ToString());

            StringReader sr = new StringReader(res);
            var to = sr.ReadToEnd();
        }


        [TestMethod]
        public void Search_XDocument()
        {
            var doc = Repository.Document();
            var data = from item in doc.Descendants("person")
                       select new
                       {
                           drink = item.Element("favoriteDrink").Value,
                           moneySpent = item.Element("moneySpent").Value,
                           zipCode = item.Element("personalInfo").Element("zip").Value
                       };
          Assert.AreEqual(5,data.Count());
          
        }

        [TestMethod]
        public void add_elem_XDocument()
        {
            var doc = Repository.Document();
            var liz = doc.Descendants("personalInfo").SingleOrDefault(c =>
                {
                    var xElement = c.Element("name");
                    return xElement != null && xElement.Value == "Liz Nelson";
                });
            if (liz != null)
            {
                var content = new XElement("Age") {Value = "36"};
                liz.Add(content);
            }
            var res = doc.ToString();
        }
    }


    public static class Repository
    {
        public static XDocument Document()
        {
            var doc = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("Starbuzz Customer Loyalty Data"),
                new XElement("starbuzzData",
                             new XAttribute("storeName", "Park Slope"),
                             new XAttribute("location", "Brooklyn, NY"),
                             new XElement("person",
                                          new XElement("personalInfo",
                                                       new XElement("name", "Janet Venutian"),
                                                       new XElement("zip", 11215)),
                                          new XElement("favoriteDrink", "Choco Macchiato"),
                                          new XElement("moneySpent", 255),
                                          new XElement("visits", 50)),
                             new XElement("person",
                                          new XElement("personalInfo",
                                                       new XElement("name", "Liz Nelson"),
                                                       new XElement("zip", 11238)),
                                          new XElement("favoriteDrink", "Double Cappuccino"),
                                          new XElement("moneySpent", 150),
                                          new XElement("visits", 35)),
                             new XElement("person",
                                          new XElement("personalInfo",
                                                       new XElement("name", "Matt Franks"),
                                                       new XElement("zip", 11217)),
                                          new XElement("favoriteDrink", "Zesty Lemon Chai"),
                                          new XElement("moneySpent", 75),
                                          new XElement("visits", 15)),
                             new XElement("person",
                                          new XElement("personalInfo",
                                                       new XElement("name", "Joe Ng"),
                                                       new XElement("zip", 11217)),
                                          new XElement("favoriteDrink", "Banana Split in a Cup"),
                                          new XElement("moneySpent", 60),
                                          new XElement("visits", 10)),
                             new XElement("person",
                                          new XElement("personalInfo",
                                                       new XElement("name", "Sarah Kalter"),
                                                       new XElement("zip", 11215)),
                                          new XElement("favoriteDrink", "Boring Coffee"),
                                          new XElement("moneySpent", 110),
                                          new XElement("visits", 15))));
            return doc;
        }
    }

    }

